﻿Shader "Shader Forge/3ColAmbient2" {
    Properties {
        _Occulsion ("贴图", 2D) = "white" {}
        _EnvTopCol ("EnvTopCol", Color) = (1.0, 1.0, 1.0, 1.0)
        _EnvMidCol ("EnvMidCol", Color) = (0.5, 0.5, 0.5, 1.0)
        _EnvBtmCol ("EnvBtmCol", Color) = (0.0, 0.0, 0.0, 1.0)
    }

    SubShader {
        Tags {
            "RenderType"="Opaque"
        }
        Pass {
            Name "FORWARD"
            Tags {
                "LightMode"="ForwardBase"
            }
            
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            #pragma multi_compile_fwdbase_fullshadows
            #pragma target 3.0

            uniform float3 _EnvTopCol;
            uniform float3 _EnvMidCol;
            uniform float3 _EnvBtmCol;
            uniform sampler2D _Occulsion;

            struct VertexInput {
                float4 vertex : POSITION;
                float3 normal : NORMAL;
                float2 uv0 : TEXCOORD0;
            };
            struct VertexOutput {
                float4 pos : SV_POSITION;
                float3 nDirWS : TEXCOORD0;
                float2 uv : TEXCOORD1;
                // LIGHTING_COORDS(0,1);
            };
            VertexOutput vert (VertexInput v) {
                VertexOutput o = (VertexOutput)0;
                o.nDirWS = UnityObjectToWorldNormal(v.normal);
                o.pos = UnityObjectToClipPos( v.vertex );
                o.uv = v.uv0;
                // TRANSFER_VERTEX_TO_FRAGMENT(o);      // 投影坐标信息
                return o;
            }
            float4 frag(VertexOutput i) : COLOR {
                float3 nDir = normalize(i.nDirWS);    // nDir
                float topMask = max(0.0, nDir.g);
                float btmMask = max(0.0, -nDir.g);
                float midMask = 1.0 - topMask - btmMask;

                float3 envCol = _EnvTopCol * topMask + _EnvMidCol * midMask + _EnvBtmCol * btmMask;

                // float shadow = LIGHT_ATTENUATION(i);

                // 采样纹理(环境光阻挡)
                float occlusion = tex2D(_Occulsion, i.uv);

                float3 envLighting = envCol * occlusion;


                return float4(envLighting, 1.0);
            }
            ENDCG
        }
    }
    FallBack "Diffuse"
    CustomEditor "ShaderForgeMaterialInspector"
}
